//
//  E_3ViewController.m
//  RACExamale
//
//  Created by cym_bj on 2017/12/1.
//  Copyright © 2017年 com.cym. All rights reserved.
//

#import "E_3ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
@interface E_3ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *TF;

@end

@implementation E_3ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"过滤";
    
    //filter: 过滤信号，使用它可以获取满足条件的信号
    //[self filter];
    
    // ignore : 忽略完某些值的信号
    //[self ignore];
    
    //take
   // [self take];
    
    //[self takeLast];
    
   // [self takeUntil];
    
   // [self distinctUntilChanged];
    
    //skip :跳跃几个值
    [self skip];
    
    
}
#pragma mark - filter: 过滤信号，使用它可以获取满足条件的信号
-(void)filter
{
    // filter: 过滤信号，使用它可以获取满足条件的信号
    //只有我们文本框的内容长度大小5，才想要获取文本框的内容
    [[self.TF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        
        //value:源信号的内容
        
        return value.length>3;
        //返回值，就是过滤条件，只有满足这个条件，才能获取到内容
    }] subscribeNext:^(NSString * _Nullable x) {
        
        NSLog(@"%@",x);
    }];
}


#pragma mark- ignore : 忽略完某些值的信号
-(void)ignore
{
    //ignore : 忽略完某些值的信号
    //内部调用 filter 过滤，忽略掉 ignore的值
    
    //ignore :忽略一些值
    //ignoreValues:忽略所有的值
    
    
    RACSubject *subject=[RACSubject subject];
    
    // 忽略所有的值
    RACSignal *ignoreSignal=[subject ignoreValues];
    
    [ignoreSignal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"%@",x);
    }];
    
    // 发送数据
    [subject sendNext:@"111"];
    [subject sendNext:@"33"];
    [subject sendNext:@"44"];
    
    //忽略掉指定的值
    [[self.TF.rac_textSignal ignore:@"4"] subscribeNext:^(NSString * _Nullable x) {
       
        NSLog(@"%@",x);
    }];
    
    
}

#pragma mark - take :从开始一共取 N 次的信号
-(void)take
{
    
    //take :取前面几个值
    RACSubject *subject=[RACSubject subject];
    
    
    [[subject take:4] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
        
    }];
    
    [subject sendNext:@"122222222"];
    [subject sendNext:@"s"];
    [subject sendNext:@"dd"];
    [subject sendNext:@"d"];
    [subject sendNext:@"s"];
    [subject sendNext:@"s"];
    [subject sendNext:@"f"];
    [subject sendNext:@"d"];
    
    [[self.TF.rac_textSignal take:5] subscribeNext:^(NSString * _Nullable x) {
       
        NSLog(@"~~~%@",x);
    }];
    
    
    
}

#pragma mark - 取后面多少个值， 必须要求发送完成
-(void)takeLast
{
    
    RACSubject *subject=[RACSubject subject];
    
    
    //取后面的三个值
    [[subject takeLast:3] subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"%@",x);
    }];
    
    [subject sendNext:@"122222222"];
    [subject sendNext:@"s"];
    [subject sendNext:@"dd"];
    [subject sendNext:@"d"];
    [subject sendNext:@"s"];
    [subject sendNext:@"s"];
    [subject sendNext:@"f"];
    [subject sendNext:@"d"];
    
    //发送完成， 不能少
    [subject sendCompleted];
    
}



#pragma mark - takeUntil:只要传入信号发送完成或者发送任意数据,就不能在接收源信号的内容
-(void)takeUntil
{
//    RACSubject *subject=[RACSubject subject];
//
//
//    subject takeUntil:<#(nonnull RACSignal *)#>
//
////    [[subject takeUntil:3] subscribeNext:^(id  _Nullable x) {
////
////        NSLog(@"%@",x);
////    }];
//
//    [subject sendNext:@"122222222"];
//    [subject sendNext:@"s"];
//    [subject sendNext:@"dd"];
//    [subject sendNext:@"d"];
//    [subject sendNext:@"s"];
//    [subject sendNext:@"s"];
//    [subject sendNext:@"f"];
//    [subject sendNext:@"d"];
    
}

#pragma mark - distinctUntilChanged:当上一次的值和当前的值有明显的变化就会发出信号，否则会被忽略掉。
-(void)distinctUntilChanged
{
    
    // 过滤，当上一次和当前的值不一样，就会发出内容。
    // 在开发中，刷新UI经常使用，只有两次数据不一样才需要刷新
    
    
    RACSubject *subject=[RACSubject subject];
    [[subject distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
       
        NSLog(@"%@",x);
    }];
    
    [subject sendNext:@"1"];
    [subject sendNext:@"2"];
    [subject sendNext:@"2"];
    
    [subject sendNext:@"3"];
    
     [subject sendNext:@"4"];
     [subject sendNext:@"4"];
    
     [subject sendNext:@"5"];
    
}

#pragma mark - skip:跳跃几个值
-(void)skip
{
    
    RACSubject *subject=[RACSubject subject];
    [[subject skip:2] subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"%@",x);
    }];
    
    [subject sendNext:@"dd"];
    [subject sendNext:@"aa"];
    [subject sendNext:@"dcc"];
    
    
    
}



@end
